/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */
 class ListNode {
      val: number
      next: ListNode | null
      constructor(val?: number, next?: ListNode | null) {
          this.val = (val===undefined ? 0 : val)
          this.next = (next===undefined ? null : next)
      }
}

 function deleteDuplicates(head: ListNode | null): ListNode | null {
    let l3: any = new ListNode() ;
    let set = new Set();
    while(head?.next||head?.val){
        set.add(head.val);
        head = head?.next;
    }

    let arr:any[] = Array.from(set);
    console.log(arr);
    if(arr.length === 0){
        return null;
    }
    
    console.log(arr);
    l3.val = arr[0]||null ;
    let l4 = l3;
    let node: ListNode;
    
    for (let i = 1; i < arr.length; i++) {
        node = new ListNode( arr[i] );
        l3.next = node;
        l3 = l3.next;
    }   
    l3.next = null;
    // while(l4.val){
    //     console.log(l4.val);
    //     l4 = l4.next;
    // }
    return l4;
};

// let head:ListNode = {val:1,next:{val:1,next:{val:2,next:null}}};
// let head = {val:1,next:{val:1,next:{val:2,next:{val:3,next:{val:4,next:null}}}}}
let head = {val:0,next:{val:0,next:{val:0,next:{val:0,next:{val:0,next:null}}}}}
console.log(deleteDuplicates(head));